自动化项目中用到到多线程,温故知新之余,我决定花点时间详细讲下Python的多线程模块threading应用。
今天先讲如何创建线程:
threading模块是在thread之上进行了封装,也是推荐使用的多线程模块,在某些版本中thread模块可能不存在,要使用dump_threading来代替threading模块。
线程创建的两种方式:
Thisclass represents an activity that is run in a separate thread of control. Thereare two ways to specify the activity: by passing a callable object to theconstructor, or by overriding the run() method in a subclass. Noother methods (except for the constructor) should be overridden in a subclass.In other words, only override the init()and run() methods of this class.
Once a thread object iscreated, its activity must be started by calling the thread’s start() method. This invokes the run() method in a separatethread of control.
直接用代码来区分吧